package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.time.Duration;

//@RestController
//@RequestMapping("/users")
@Slf4j
public class MyRestController {

    /**
     * 线程变量    跨同步代码块就取不到了  (应该是因为线程切换走了)
     */
    static ThreadLocal<String> MSG = new ThreadLocal<>();

    public static Mono<ServerResponse> test(ServerRequest request) {
        String threadName1 = Thread.currentThread().getName();
        MSG.set("11111" + threadName1);
        log.info("MSG.get={}", MSG.get());
        return Mono.delay(Duration.ofMillis(500))
                .then(Mono.fromRunnable(() -> {
                    String threadName2 = Thread.currentThread().getName();
                    log.info("threadName1={},threadName2={},msg={}", threadName1, threadName2, MSG.get());
//                    MSG.remove();
                }).then(ServerResponse.ok().body(Mono.just("test-web-flux"), String.class)));
    }
}
